home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / MPW C ƒ / NetTimeProtocol ƒ / ntpsubs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-23  |  9.2 KB  |  459 lines  |  [TEXT/MPS ]

  1. #ifndef lint
  2. static char *RCSid = "$Header: ntpsubs.c,v 2.14 89/02/19 16:08:52 bww Exp $";
  3. #endif
  4.  
  5. /*
  6.  ****************************************************************
  7.  * HISTORY
  8.  * $Log:    ntpsubs.c,v $
  9.  * Revision 2.14  89/02/19  16:08:52  bww
  10.  *     SSP headers.
  11.  *     [89/02/19  16:07:17  bww]
  12.  * 
  13.  * Revision 2.13  88/11/17  22:30:41  bww
  14.  * Allow for negative packet delays.
  15.  * 
  16.  * Revision 2.12  88/11/11  16:07:15  bww
  17.  * Upgraded to latest UMD release.
  18.  * 
  19.  * Revision 2.11  88/10/03  23:30:39  bww
  20.  * Revamped getinaddr() to return useful information
  21.  * for multi-homed hosts (at least around here).
  22.  * 
  23.  * Revision 2.10  88/09/20  21:59:48  bww
  24.  * Fixed double to u_long conversions.
  25.  * 
  26.  * Revision 2.9  88/08/25  14:08:51  bww
  27.  * More rationalizations.
  28.  * 
  29.  * Revision 2.8  88/08/22  16:48:57  bww
  30.  * Rationalized type conversion fixes.
  31.  * 
  32.  * Revision 2.7  88/08/22  00:59:52  bww
  33.  * Fixed falsetick() to only sort once.
  34.  * 
  35.  * Revision 2.6  88/08/20  19:04:10  bww
  36.  * Fixed some "cannot happens" that were happening.
  37.  * 
  38.  * Revision 2.5  88/08/20  17:00:04  bww
  39.  * Moved getinaddr() here.
  40.  * 
  41.  * Revision 2.4  88/08/18  21:29:01  bww
  42.  * Fixed handling of signed octets to be machine independent.
  43.  * 
  44.  * Revision 2.3  88/07/17  15:07:01  bww
  45.  * First CMUCS release
  46.  * 
  47.  * Revision 2.2  88/05/13  10:47:34  root
  48.  * Initial CMU revision
  49.  * 
  50.  * Revision 2.2  88/05/13  10:47:34  root
  51.  * *** empty log message ***
  52.  * 
  53.  * Revision 2.1  88/04/25  18:00:26  root
  54.  * *** empty log message ***
  55.  * 
  56.  * Revision 2.0  88/03/31  10:25:27  root
  57.  * *** empty log message ***
  58.  * 
  59.  * Revision 1.2  88/02/28  22:59:49  petry
  60.  * *** empty log message ***
  61.  * 
  62.  * Revision 1.1  87/12/16  10:33:21  root
  63.  * Initial revision
  64.  * 
  65.  */
  66.  
  67. #include    <Types.h>
  68. #include    <Math.h>
  69. #include     <fcntl.h>
  70. #include     <stdio.h>
  71. #include    <ErrMgr.h>
  72. #include    <CursorCtl.h>
  73. #include    <Errors.h>
  74. #include    <Devices.h>
  75. #include    <MacTCPCommonTypes.h>
  76. #include    <UDPPB.h>
  77. #include    <TCPPB.h>
  78. #include     <GetMyIPAddr.h>
  79. #include    <AddressXlation.h>
  80. #include    <Time.h>
  81. #include    <Script.h>
  82. #define     fraction  fraction /* the script manager does this */
  83.  
  84. #include     "ntp.h"
  85.  
  86.  
  87. #ifdef DEBUG
  88. extern int debug;
  89. #endif
  90.  
  91. #define BIT7    ((u_int) (1<<7))
  92. #define BIT15    ((u_int) (1<<15))
  93. #define BIT31    ((u_long) (1<<31))
  94.  
  95. #define TWO16    ((double) 65536.0)
  96. #define    TWO31    ((double) 2.147483648e9)
  97. #define    TWO32    ((double) 4.294967296e9)
  98.  
  99. #define signed8(b)    ((b) & BIT7)
  100. #define signed16(b)    ((b) & BIT15)
  101. #define signed32(b)    ((b) & BIT31)
  102.  
  103. #define lshift16(a)    ((a) *= TWO16)
  104. #define lshift32(a)    ((a) *= TWO32)
  105.  
  106. #define rshift16(a)    ((a) /= TWO16)
  107. #define rshift32(a)    ((a) /= TWO32)
  108.  
  109. /*
  110.  * some compilers seem to have trouble with these conversions
  111.  * when the top bit is on, so let's spell it out
  112.  */
  113. #define    double_to_u_long(a)    ((TWO31<=(a)&&(a)<TWO32)?\
  114.                 ((u_long)((a)-TWO31))|BIT31:(u_long)(a))
  115. #define u_long_to_double(b)    (signed32(b)?((b)^BIT31)+TWO31:(double)(b))
  116.  
  117. double
  118. ul_fixed_to_double(t)
  119.     struct l_fixedpt *t;
  120. {
  121.     double a;
  122.     u_long b;
  123.  
  124.     b = ntohl(t->fraction);
  125.     a = u_long_to_double(b);
  126.     rshift32(a);
  127.     b = ntohl(t->int_part);
  128.     a += u_long_to_double(b);
  129.     return (a);
  130. }
  131.  
  132. double
  133. l_fixed_to_double(t)
  134.     struct l_fixedpt *t;
  135. {
  136.     double a;
  137.     u_long b;
  138.  
  139.     if (signed32(ntohl(t->int_part))) {
  140.         b = ntohl(~t->fraction);
  141.         a = u_long_to_double(b);
  142.         rshift32(a);
  143.         a += ntohl(~t->int_part);
  144.         a = -a;
  145.     } else {
  146.         b = ntohl(t->fraction);
  147.         a = u_long_to_double(b);
  148.         rshift32(a);
  149.         a += ntohl(t->int_part);
  150.     }
  151.     return (a);
  152. }
  153.  
  154. double
  155. s_fixed_to_double(t)
  156.     struct s_fixedpt *t;
  157. {
  158.     double a;
  159.  
  160.     if (signed16(ntohs(t->sint_part))) {
  161.         a = ntohs(~t->sfraction & 0xffff);
  162.         rshift16(a);
  163.         a += ntohs(~t->sint_part & 0xffff);
  164.         a = -a;
  165.     } else {
  166.         a = ntohs(t->sfraction);
  167.         rshift16(a);
  168.         a += ntohs(t->sint_part);
  169.     }
  170.     return (a);
  171. }
  172.  
  173. double_to_l_fixed(t, value)
  174.     struct l_fixedpt *t;
  175.     double value;
  176. {
  177.     double temp;
  178.  
  179.     if (value < 0.0) {
  180.         value = -value;
  181.         t->int_part = double_to_u_long(value);
  182.         temp = value - t->int_part;
  183.         lshift32(temp);
  184.         t->fraction = double_to_u_long(temp);
  185.         t->int_part = htonl(~t->int_part);
  186.         t->fraction = htonl(~t->fraction);
  187.     } else {
  188.         t->int_part = double_to_u_long(value);
  189.         temp = value - t->int_part;
  190.         lshift32(temp);
  191.         t->fraction = double_to_u_long(temp);
  192.         t->int_part = htonl(t->int_part);
  193.         t->fraction = htonl(t->fraction);
  194.     }
  195. }
  196.  
  197. double_to_s_fixed(t, value)
  198.     struct s_fixedpt *t;
  199.     double value;
  200. {
  201.     double temp;
  202.  
  203.     if (value < 0.0) {
  204.         value = -value;
  205.         t->sint_part = value;
  206.         temp = value - t->sint_part;
  207.         lshift16(temp);
  208.         t->sfraction = temp;
  209.         t->sint_part = htons(~t->sint_part);
  210.         t->sfraction = htons(~t->sfraction);
  211.     } else {
  212.         t->sint_part = value;
  213.         temp = value - t->sint_part;
  214.         lshift16(temp);
  215.         t->sfraction = temp;
  216.         t->sint_part = htons(t->sint_part);
  217.         t->sfraction = htons(t->sfraction);
  218.     }
  219. }
  220.  
  221. int
  222. char_to_int(c)
  223.     int c;
  224. {
  225.     if (signed8(c))
  226.         return ((~0<<8) | c);  /* sign extend */
  227.     return (c);
  228. }
  229.  
  230. int_to_char(p, i)
  231.     char *p;
  232.     int i;
  233. {
  234.     *p = i;
  235. }
  236.  
  237. /* BEGIN MAC SPECIFIC FUNCTIONS */
  238. /* gettimeofday is a unix function that is supposed to return the time of day 
  239.  * in seconds since Jan 1, 1970.  Mac time is returned in seconds since 
  240.  * Jan 1, 1900.
  241.  * We do the conversion of seconds here.
  242.  */
  243.  
  244. #define YEARDIFF (66)
  245. #define SECSperMINUTE (60)
  246.  
  247. static long gmtDelta = 0; 
  248.  
  249. gettimeofday(tp,tzp)
  250. struct timeval *tp;
  251. struct timezone *tzp;
  252. {
  253. #pragma unused(tzp)
  254.  
  255.     time_t            thetime;
  256.     struct tm        *thetm;
  257.     
  258.  
  259.     time(&thetime);
  260.     thetm = localtime(&thetime);
  261.     //fprintf(stderr,"gettimeofday: given %s\n",asctime(thetm));
  262.  
  263.     /* subtract YEARDIFF years */
  264.     thetm->tm_year -= YEARDIFF;
  265.     /* convert back to seconds */
  266.     thetime    = mktime(thetm);
  267.     
  268.     /* convert to GMT */
  269.     thetime -= (gmtDelta);
  270.  
  271.     tp->tv_sec  = thetime;
  272.     tp->tv_usec = 0; /* on the mac,always */
  273.     
  274.     //fprintf(stderr,"gettimeofday: %s\n",asctime(localtime(&tp->tv_sec)));
  275. }
  276.  
  277. static time_t unixtomacsecs(const time_t *unixsecs)
  278. {
  279.     time_t            thetime;
  280.     struct tm        *thetm;
  281.  
  282.     thetime = *unixsecs;
  283.     
  284.     thetm = localtime(&thetime);
  285.  
  286.     //fprintf(stderr,"unixtomacsecs: given %s\n",asctime(thetm));
  287.  
  288.     /* add YEARDIFF years */
  289.     thetm->tm_year += YEARDIFF;
  290.     /* convert back to seconds */
  291.     thetime    = mktime(thetm);
  292.  
  293.     /* convert to GMT */
  294.     thetime += (gmtDelta);
  295.  
  296.     //fprintf(stderr,"unixtomacsecs: %s\n",asctime(localtime(&thetime)));
  297.  
  298.     return thetime;
  299. }
  300.  
  301. settimeofday(tp,tzp)
  302. struct timeval *tp;
  303. struct timezone *tzp;
  304. {
  305. #pragma unused(tzp)
  306.     time_t            thetime;
  307.  
  308.     thetime = unixtomacsecs(&(tp->tv_sec));
  309.     
  310.     /* set the clock */
  311.     
  312.     if (SetDateTime(thetime) != noErr) {
  313.         return (-1);
  314.     }
  315. }
  316.  
  317. void setminuteseast(long num)
  318. {
  319.     MachineLocation loc;
  320.     long internalgmtdelta;
  321.     
  322.  
  323.     if (num) {
  324.         gmtDelta = num * SECSperMINUTE;
  325.     } else {
  326.         /* get from the PRAM */
  327.         
  328.         ReadLocation(&loc);
  329.         internalgmtdelta = loc.gmtFlags.gmtDelta & 0x00ffffff;
  330.         
  331.         if ( (internalgmtdelta >> 23) & 1) { /* need to sign extend */
  332.             internalgmtdelta = internalgmtdelta | 0xff000000;
  333.         }
  334.         
  335.         gmtDelta = internalgmtdelta;
  336.         if (loc.gmtFlags.dlsDelta != 0) {
  337.             fprintf(stderr,"Warning: not using Daylight Savings flag (value = %u)\n",loc.gmtFlags.dlsDelta);
  338.         }
  339.     }
  340. }
  341.  
  342. char *
  343. macctime(time_t *unixsecs)
  344. {
  345.     time_t            thetime;
  346.  
  347.     thetime = unixtomacsecs(unixsecs);
  348.     
  349.     return asctime(localtime(&thetime));
  350. }
  351.  
  352.  
  353. /* END MAC SPECIFIC FUNCTIONS */
  354.  
  355.  
  356. tstamp(stampp, tvp)
  357.     struct l_fixedpt *stampp;
  358.     struct timeval *tvp;
  359. {
  360.     double temp;
  361.  
  362.     stampp->int_part = JAN_1970 + tvp->tv_sec;
  363.     temp = u_long_to_double(tvp->tv_usec) / 1e6;
  364.     lshift32(temp);
  365.     stampp->fraction = double_to_u_long(temp);
  366.     stampp->int_part = htonl(stampp->int_part);
  367.     stampp->fraction = htonl(stampp->fraction);
  368. }
  369.  
  370. double
  371. minimum_filter(delay, offset, count)
  372.     register double *delay;
  373.     register double *offset;
  374.     register int count;
  375. {
  376.     register int i, j;
  377.     double temp, dsp;
  378.  
  379.     for (i = 0; i < count - 1; i++) {
  380.         dsp = fabs(delay[i]);
  381.         for (j = i + 1; j < count; j++) {
  382.             if (dsp > fabs(delay[j])) {
  383.                 temp = delay[i];
  384.                 delay[i] = delay[j];
  385.                 delay[j] = temp;
  386.                 dsp = fabs(delay[i]);
  387.                 temp = offset[i];
  388.                 offset[i] = offset[j];
  389.                 offset[j] = temp;
  390.             }
  391.         }
  392.     }
  393.     /* we are now sorted by |delay| */
  394.     dsp = 0.0;
  395.     for (i = count - 1; i > 0; i--) {
  396.         temp = (fabs(delay[i]) >= NTP_MAXDELAY) ?
  397.             NTP_MAXDISP : (offset[i] - offset[0]);
  398.         if (temp < 0.0)
  399.             temp = -temp;
  400.         if (temp > NTP_MAXDISP)
  401.             temp = NTP_MAXDISP;
  402.         dsp /= 2.0;
  403.         dsp += temp;
  404.     }
  405.     dsp /= 2.0;
  406.     return (dsp);
  407. }
  408.  
  409. falsetick(sl, count)
  410.     register struct select_list *sl;
  411.     register int count;
  412. {
  413.     register int i, j, maxind;
  414.     struct select_list temp;
  415.     double diff, dsp, off;
  416.  
  417.     for (i = 0; i < count - 1; i++) {
  418.         for (j = i + 1; j < count; j++) {
  419.             if (sl[i].sort_item > sl[j].sort_item) {
  420.                 temp = sl[i];
  421.                 sl[i] = sl[j];
  422.                 sl[j] = temp;
  423.             }
  424.         }
  425.     }
  426.     /* we are now sorted by sort_item */
  427.     do {
  428.         for (i = 0; i < count; i++) {
  429.             dsp = 0.0;
  430.             off = sl[i].peer->offset;
  431.             for (j = count - 1; j >= 0; j--) {
  432.                 diff = sl[j].peer->offset - off;
  433.                 if (diff < 0.0)
  434.                     diff = -diff;
  435.                 if (diff > NTP_MAXDISP)
  436.                     diff = NTP_MAXDISP;
  437.                 dsp = (dsp * PEER_SELECT) + diff;
  438.             }
  439.             sl[i].dsp = dsp * PEER_SELECT;
  440. #ifdef DEBUG
  441.             if (debug > 1)
  442.                 printf("sl[%d] = %s %x %f\n",
  443.                     i,
  444.                     /* inet_ntoa((sl[i].peer)->src.sin_addr) */ "??",
  445.                     sl[i].sort_item,
  446.                     sl[i].dsp);
  447. #endif
  448.         }
  449.         /*
  450.          * Remove entry with the largest dispersion
  451.          */
  452.         for (maxind = 0, i = 1; i < count; i++)
  453.             if (sl[i].dsp >= sl[maxind].dsp)
  454.                 maxind = i;
  455.         for (i = maxind + 1; i < count; i++)
  456.             sl[i - 1] = sl[i];
  457.     } while (--count > 0);
  458. }
  459.